<?php
/*
 * @南联便利
 * @功能说明：系统配置参数
 * @更新说明：暂无更新
 * @文件名 ConfigController.class.php
 * @编码 UTF-8
 * @创建时间 2015-9-22 下午2:00:20
 * @创建人 XING
 */
namespace home\Controller;
use Think\Controller;

class ConfigController extends ApibaseController {
	/*
	 * 系统设置
	 * @date: 2015-4-22下午4:50:02
	 * @editor: XING
	 */
	public function set(){
		/*参数初始化*/
		$page = I('get.page') ?: 1;
		$perpage = 20;$url = array();
		$list = M('ConfigSet')->where(array('is_config'=>'0'));
		if(I('get.name')){
			$list = $list->where('config_desc LIKE "%'.I('get.name').'%"');
			$url['name'] = I('get.name');
		}
		$getData = $list->order(array('is_system'=>'desc','add_at'=>'desc'))->page($page,$perpage)->select();
		
		$this->assign('page',setPage($list->count(),$perpage,$page,'?'.http_build_query($url)));
		$this->assign('getData',$getData);
		$this->display('config/set');
	}
	
	
	/*
	 * 添加版本
	 * @date: 2015-4-21下午2:18:20
	 * @editor: XING
	 */
	public function addset(){
		if(I('post.dosubmit')){
			if(I('post.id')){
				$setUpdate = array(
					'update_at' => time()
				);

				I('post.config_column') ? $setUpdate['config_column'] = trim(I('post.config_column')) : '';
				I('post.config_desc') ? $setUpdate['config_desc'] = trim(I('post.config_desc')) : '';
				$setUpdate['config_val'] = I('post.video_url').'#'.I('post.more_url').'#'.I('post.config_val');
				M('ConfigSet')->where(array('id'=>I('post.id')))->setField($setUpdate);
				if(I('post.setid') == 'passKf'){
					$sql = 'UPDATE yd_cust_entity SET cust_pass = MD5(CONCAT(cust_mobile,"'.trim(I('post.config_val')).'")) WHERE for_kf = 0';
					(new \Think\Model())->execute($sql);
				}
				$this->showtips('变量更新成功');
			}
			$isExist = M('ConfigSet')->where(array('config_column'=>I('post.config_column')))->count();
			if($isExist > 0){
				$this->showtips('该变量已存在，操作失败');
			}else{
				$addId = M('ConfigSet')->add(array(
							'config_column' => I('post.config_column'),
							'config_val' => I('post.config_val'),
							'config_desc' => I('post.config_desc'),
							'add_at' => time(),
							'update_at' => time()
						));
				if($addId){
					putlog('添加系统变量  ID`'.$addId.'`','ConfigController','addset');
					$this->showtips('添加变量成功');
				}else{
					$this->showtips('操作失败');
				}
			}
		}
		
		$getData = M('ConfigSet')->where(array('id'=>I('get.id')))->find();
		$explode = explode('#', $getData['config_val']);
		$getData['video_url'] = trim($explode[0]);
		$getData['more_url']  = $explode[1];
		$getData['config_val']  = $explode[2];
		// var_dump($getData);exit;
		$this->assign('getData',$getData);
		$this->display('config/addset');
	}
	
	/*
	 * 版本删除
	 * @date: 2015-4-21下午2:37:04
	 * @editor: XING
	 */
	public function delete(){
		if(!(I('get.id'))){
			$this->showtips('参数不完整，操作失败');
		}
		M('ConfigSet')->where(array('id'=>I('get.id')))->delete();
		$this->showtips('删除成功');
	}
	
	/*
	 * 灯商云端_缓存管理
	 * @date: 2015-8-10 下午5:36:21
	 * @editor: XING
	 */
	public function cacheSet(){
		header("Content-type: text/html; charset=utf-8");
		$inCache = array_keys(S('gbcc'));
		//其他配置
		$getTableParams = (new \Think\Model())->query('show tables');
		//取得所有的表名
		foreach ($getTableParams AS $row) {
			if(in_array($row['tables_in_yd_server'],$inCache)){
				$tables[]['table_name'] = $row['tables_in_yd_server'];
			}
		}
		//循环取得所有表的备注及表中列消息
		foreach ($tables AS $k=>$v) {
			$sql  = 'SELECT * FROM ';
			$sql .= 'INFORMATION_SCHEMA.TABLES ';
			$sql .= 'WHERE ';
			$sql .= "table_name = '{$v['table_name']}'";
			$table_result = (new \Think\Model())->query($sql);
			$tables[$k]['table_comment'] = $table_result[0]['table_comment'];
		}
		
		$this->assign('inCache',S('gbcc'));
		$this->assign('MCache',$tables);
		$this->display('config/cache');
	}
	
	/*
	 * 灯商云端_缓存更新
	 * @date: 2015-8-10 下午6:37:08
	 * @editor: XING
	 */
	public function crefresh(){
		if(I('get.cid')){
			$topCaches = S('gbcc');
			if($topCaches[trim(I('get.cid'))]){
				$affectNum = \Think\Cache::getInstance()->rm($topCaches[trim(I('get.cid'))]);
				unset($topCaches[trim(I('get.cid'))]);
				S('gbcc',$topCaches);
				$this->showtips('缓存更新成功');
			}else{
				$this->showtips('操作失败');
			}
		}elseif(I('get.doClear')){
			if(\Think\Cache::getInstance()->clear()){
				$this->showtips('缓存更新成功');
			}else{
				$this->showtips('操作失败');
			}
		}else{
			$this->showtips('无效参数');
		}
	}
}